<?php include(APP_HOME."/MPDF56/mpdf.php");

if(!class_exists('MAIN_Controller_Action')) Zend_Loader :: loadClass('MAIN_Controller_Action');
class Front_PrintController extends MAIN_Controller_Action {
	public function printpreauthAction() {
		$view = $this->_getView();
		$params   = $this->getParams();
		$generic = $this->getGeneric();
	
		//_print( $params); exit();
	
		$today = getToday();
		$preauthId = $generic->getPreauthId($params['policy_id']);
		$policyData = $generic->getPolicyById($params['policy_id']);
		$paInfo = $generic->getPaInfo($params['policy_id']);
		$paClaimData = $generic->getPaClaim($params['policy_id']);
		$OPDClaimData = $generic->getPaClaimOpd($params['policy_id']);
		$erClaimData = $generic->getErClaim($params['policy_id']); // use $html2
		$PlanData = $generic->getPlan2catByPlanId($policyData['plan_id'],$params['policy_id']);	// use $html2
		$PlanData['opd'] = number_format($PlanData['opd'],2,'.',',');
		$PlanData['rb'] = number_format($PlanData['rb'],2,'.',',');
		$PlanData['er'] = number_format($PlanData['er'],2,'.',',');
	
		if($policyData['polstatus_desc'] == "ไม่มีผลบังคับ"){
			$polstatus_color	=	"red";
		} else {
			$polstatus_color	=	"green";
		}
		$css_path = APP_URL.'/modules/systemapi/templates/default/css/print.css';
		$img_path =	APP_URL.'/modules/systemapi/templates/default/images';
		//_print($img_path)
	
		if($policyData['customer_type_desc'] ==	"PA"){
			$img = 'title_pa.png';
		} else if($policyData['customer_type_desc']	==	"Health"){
			$img = 'title_health.png';
		}

		$view->assign("preauthId",$preauthId);
		$view->assign("policyData",$policyData);
		$view->assign("paInfo",$paInfo);
		$view->assign("paClaimData",$paClaimData);
		$view->assign("OPDClaimData",$OPDClaimData);
		$view->assign("erClaimData",$erClaimData);	// use $html2
		$view->assign("PlanData",$PlanData);	// use $html2
		$view->assign("img_path",$img_path);
		$view->assign("img",$img);

	
		if($policyData['customer_type_desc']	==	"PA"){
			$view->output('print/paprint.tpl');
		} else if($policyData['customer_type_desc']	==	"Health"){
			$view->output('print/hsprint.tpl');
		}
		
		$var = base64_encode($var);
		$DataRet = $generic->updatePreauthImage($preauthId,$var);
	}
	
	
	public function printpdfAction() {
		$view = $this->_getView();
		$auth = Zend_Auth :: getInstance();
		$params   = $this->getParams();
		$generic = $this->getGeneric();
		//_print( $params['preauth_id']); exit();
		//_print( $params['policy_id']); exit();
		$identity = $auth->getIdentity();
		$today = getToday();
		$preauthId = $generic->getPreauthId($params['policy_id']);
    	$policyData = $generic->getPolicyById($params['policy_id']);   	
    	$paInfo = $generic->getPaInfo($params['policy_id']);
    	$paClaimData = $generic->getPaClaim($params['policy_id']);
    	$OPDClaimData = $generic->getPaClaimOpd($params['policy_id']);
    	$erClaimData = $generic->getErClaim($params['policy_id']);					// use $html2
    	$PlanData = $generic->getPlan2catByPlanId($policyData['plan_id'],$params['policy_id']);	// use $html2 
    	$PlanData['opd'] = number_format($PlanData['opd'],2,'.',',');
    	$PlanData['rb'] = number_format($PlanData['rb'],2,'.',',');
    	$PlanData['er'] = number_format($PlanData['er'],2,'.',',');
    	$deducList = $generic->getDeducList($params['policy_id']);
    	
    	if(empty($deducList)){
    		$deducList[0]['deducper'] = '-';
    		$deducList[0]['dgroup'] = $policyData[effective];
    		$deducList[0]['deduc'] = number_format($policyData[deductible],2,'.',',');
    	}
    	else{
    		foreach ($deducList as $key=>$value){
    			$deducList[$key]['deduc'] = number_format(($policyData[deductible] - $deducList[$key]['deduc']),2,'.',',');
    			 
    		}
    	}
    	
		//$img_path	=	APP_HOME.'/modules/systemapi/templates/default/images';
    	$img_path	=	APP_URL.'/modules/systemapi/templates/default/images';

		//_print($policyData); exit();
		$view->assign("preauthId",$preauthId);
		$view->assign("policyData",$policyData);
		$view->assign("paInfo",$paInfo);
		$view->assign("paClaimData",$paClaimData);
		$view->assign("OPDClaimData",$OPDClaimData);
		$view->assign("erClaimData",$erClaimData); // use $html2
		$view->assign("PlanData",$PlanData); // use $html2

		if($policyData['customer_type_desc']	==	"PA"){
			$img = 'title_pa.png';
		} else if($policyData['customer_type_desc']	==	"Health"){
			$img = 'title_health.png';
		}

		if($policyData['polstatus_desc'] == "ไม่มีผลบังคับ"){
		 	$polstatus_color	=	"red";
		} else {
			$polstatus_color	=	"green";
		}

	$html = '
		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
		<html xmlns="http://www.w3.org/1999/xhtml">
		<head>
		<meta http-equiv="Content-Type" content="text/html; charset=TIS-620" />
		</head>
		<body style="background-color:#ffffff; " onload="window.print();">
			<div id="print_preauth_PA_head" style="border-style:solid; border-width:0px; width:700px; border-color: Red; ">
				<table width="700" valign="top" border="1" cellpadding="3" cellspacing="0" style="font-size:14px;" >
					<tr> 
						<td width="100" style="text-align:right; vertical-align:bottom;"> ระบบ :</td>
						<td width="145" style="text-align:left; vertical-align:bottom;"> Pre-authorize</td>				
						<td width="250" rowspan="2" valign="middle" align="center" style="font-size:18px;"> Health Bebefit Consultants </td>
						<td width="100" rowspan="4" valign="middle" align="center"style="font-size:18px;"><img src="'.$img_path.'/logo.png" </td>
					</tr>
					<tr>
						<td width="100" style="text-align:right; vertical-align:bottom;"> หน่วยงาน :</td>
						<td width="145" style="text-align:left;">'.$identity->group_name.'</td>
						
					</tr>
					<tr> 
						<td width="95" align="right" valign="middle"> วันทำรายการ :</td>
						<td width="160" style="text-align:left;">'.$today.'</td>
						<td width="240" rowspan="2" valign="middle" align="center" style="font-size:22px;"> Pre-auth ID: <B>'.$preauthId.'</B></td>
					</tr>
					<tr> 
						<td width="100" style="text-align:right; vertical-align:bottom;"> ประเภทกรมธรรม์ :</td>
						<td width="145" style="text-align:left;"><B>PA</B></td>
					</tr>			
				</table>
			</div>
			
			<div id="print_preauth_PA_PolicyINFO" style="border-style:solid; border-width:0px; width:700px; border-color: blue; ">
				<table width="700" valign="top" border="1" cellpadding="3" cellspacing="0" style="font-size:14px; margin-top:15px;" >
					<tr> 
						<td align="center" colspan="4" style="font-size:16px;" ><U><B> ข้อมูลกรมธรรม์ </B></U></td>			
					</tr>
					<tr> 
						<td width="180" style="text-align:right;">ชื่อผู้เอาประกัน :</td>
						<td width="170" style="text-align:left;">'.$policyData['name'].' '.$policyData['surname'].' </td>
						<td width="150" style="text-align:right;">บัตรประชาชน :</td>
						<td width="200" style="text-align:left;">'.$policyData['natid'].' </td>			
					</tr>
					<tr> 
						<td style="text-align:right;">เลขที่กรมธรรม์ :</td>
						<td style="text-align:left;"> '.$policyData['policy_no'].'</td>
						<td style="text-align:right;">เลขที่บัตรประกัน :</td>
						<td style="text-align:left;"> '.$policyData['cardno'].'</td>			
					</tr>
					<tr> 
						<td style="text-align:right;">วันเริ่มคุ้มครอง :</td>
						<td style="text-align:left;">'.$policyData['effective'].' </td>
						<td style="text-align:right;">วันสิ้นสุดความคุ้มครอง :</td>
						<td style="text-align:left;">'.$policyData['expiry'].' </td>			
					</tr>
					<tr> 
						<td style="text-align:right;">แผนความคุ้มครอง :</td>
						<td colspan="3" style="text-align:left;"> '.$policyData['plan'].'</td>									
					</tr>
					<tr>
						<td align="right">Deductible :</td>
						<td>'.number_format($policyData['deductible'],2,'.',',').'-'.$paInfo['deducper'].'</td>
						<td align="right">Co-Payment :</td>
						<td>'.$policyData['copayment'].'</td>
					</tr>
					<tr>
						<td valign="top" align="right" >รายการ Deductible คงเหลือ :</td>	
						<td colspan="3" align="left">
							<div id="tbl-DeducList_container">
							<table width="70%" align="left" border="0" cellpadding="0" cellspacing="0" >';
	
							if($deducList[0]['deduc'] == 0){
						$html.='<tr>
									<td align="left" >ไม่มี Deductible คงเหลือ</td>
								</tr>';
							}else{
								foreach( $deducList as $value ) {
						$html.='<tr>
									<td align="left" >'.$value['dgroup'].'=> ( คงเหลือ '.$value['deduc'].' )</td>							
								</tr>';
								}
							}
							
					$html.='</table>
							</div>
						</td>
					</tr>	
					<tr> 
						<td colspan="4" style="text-align:center;"> '.$policyData['memc_cover'].'</td>
					</tr>
					<tr> 
						<td style="text-align:right;">สถานะกรมธรรม์ :</td>
						<td colspan="3" style="text-align:left;"><B>'.$policyData['polstatus_desc'].'</B> </td>								
					</tr>
				</table>
			</div>
			
			<div id="print_preauth_PA_PolicyRule" style="border-style:solid; border-width:0px; width:700px; border-color: green; ">
				<table width="700" align="center" border="1" cellpadding="3" cellspacing="0" style="font-size:14px; margin-top:15px;">
					<tr>			
						<td align="center" style="font-size:16px;" ><U><B>เงื่อนไขกรมธรรม์</B></U></td>
					</tr>	
					<tr>
						<td height="150" align="center" valign="top">'.$paInfo['policy_condition'].'</td>
					</tr>
				</table>
			</div>
			
			<div id="print_preauth_PA_History" style="border-style:solid; border-width:0px; width:700px; border-color: yellow; ">
				<table width="700" align="center" border="1" cellpadding="3" cellspacing="0" style="font-size:14px; margin-top:15px;" >					
					<tr>
						<td colspan="3" align="center" style="font-size:16px;"><U><B>ประวัติการรักษา </B></U></td>
					</tr>
					<tr>
						<td width="200" align="center" valign="bottom">วันเกิดอุบัติเหตุ</td>
						<td width="200" align="center" valign="bottom">ค่าใช้จ่าย</td>
						<td align="center" valign="bottom">ICD10</td>
					</tr>';
					foreach( $paClaimData as $value ) {
			$html.= '<tr>
						<td align="center">'.$value['acc_date'].'	</td>
						<td align="center">'.$value['paid'].' บาท	</td>
						<td align="center">'.$value['diags'].'		</td>
					</tr>
					';
					}
			$html.='
				</table>
			</div>
			
			<div id="print_preauth_PA_Note" style="border-style:solid; border-width:0px; width:700px; border-color: Red; ">
				<table width="700" align="center" border="1" cellpadding="3" cellspacing="0" style="font-size:14px; margin-top:15px;" >
					<tr>
						<td align="center" style="font-size:16px;"><U><B>บันทึกเกี่ยวกับ ผอป.</B></U></td>
					</tr>
					<tr>
						<td height="150" align="center" valign="top">'.$paInfo['note2hosp'].'</td>
					</tr>
				</table>
			</div>
		
		</body>
		</html>
';
	
	
	$html2 = '
		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
		<html xmlns="http://www.w3.org/1999/xhtml">
		<head>
		<meta http-equiv="Content-Type" content="text/html; charset=TIS-620" />
		</head>
		<body style="background-color:#ffffff;" onload="window.print();" >	

			<div id="print_preauth_HEALTH_head" style="border-style:solid; border-width:0px; width:700px; border-color: Red; ">
				<table width="700" valign="top" border="1" cellpadding="3" cellspacing="0" style="font-size:14px; " >
					<tr> 
						<td width="95" style="text-align:right; vertical-align:bottom;"> ระบบ :</td>
						<td width="160" style="text-align:left; vertical-align:bottom;"> Pre-authorize</td>				
						<td width="240" rowspan="2" valign="middle" align="center" style="font-size:18px;"> Health Bebefit Consultants </td>
						<td width="100" rowspan="4" valign="middle" align="center"style="font-size:18px;"><img src="'.$img_path.'/logo.png" </td>
					</tr>
					<tr>
						<td width="95" style="text-align:right; vertical-align:bottom;"> หน่วยงาน :</td>
						<td width="150" style="text-align:left;">'.$identity->group_name.'</td>
						
					</tr>
					<tr> 
						<td width="95" align="right" valign="middle"> วันทำรายการ :</td>
						<td width="160" style="text-align:left;">'.$today.'</td>
						<td width="240" rowspan="2" valign="middle" align="center" style="font-size:22px;"> Pre-auth ID: <B>'.$preauthId.'</B></td>
					</tr>
					<tr> 
						<td width="95" style="text-align:right; vertical-align:bottom;"> ประเภทกรมธรรม์ :</td>
						<td width="150" style="text-align:left;"><B>HEALTH</B></td>
					</tr>			
				</table>
			</div>
			
			<div id="print_preauth_HEALTH_PolicyINFO" style="border-style:solid; border-width:0px; width:700px; border-color: blue; ">
				<table width="700" valign="top" border="1" cellpadding="3" cellspacing="0" style="font-size:14px; margin-top:15px;" >
					<tr> 
						<td align="center" colspan="4" style="font-size:16px;" ><U><B> ข้อมูลกรมธรรม์ </B></U></td>			
					</tr>
					<tr> 
						<td width="180" style="text-align:right;">ชื่อผู้เอาประกัน :</td>
						<td width="170" style="text-align:left;">'.$policyData['name'].' '.$policyData['surname'].' </td>
						<td width="150" style="text-align:right;">บัตรประชาชน :</td>
						<td width="200" style="text-align:left;">'.$policyData['natid'].' </td>			
					</tr>
					<tr> 
						<td style="text-align:right;">เลขที่กรมธรรม์ :</td>
						<td style="text-align:left;"> '.$policyData['policy_no'].'</td>
						<td style="text-align:right;">เลขที่บัตรประกัน :</td>
						<td style="text-align:left;"> '.$policyData['cardno'].'</td>			
					</tr>
					<tr> 
						<td style="text-align:right;">วันเริ่มคุ้มครอง :</td>
						<td style="text-align:left;">'.$policyData['effective'].' </td>
						<td style="text-align:right;">วันสิ้นสุดความคุ้มครอง :</td>
						<td style="text-align:left;">'.$policyData['expiry'].' </td>
					</tr>
					<tr> 
						<td style="text-align:right;">แผนความคุ้มครอง :</td>
						<td colspan="3" style="text-align:left;"> '.$policyData['plan'].'</td>									
					</tr>
					<tr>
						<td align="right">OPD :</td>
						<td>'.$PlanData['opd'].'</td>
						<td align="right">ER :</td>
						<td>'.$PlanData['er'].'</td>
					</tr>
					<tr>
						<td align="right">OPD คงเหลือ :</td>
						<td>'.$PlanData['opdremain'].'</td>
						<td align="right">R&B :</td>
						<td>'.$PlanData['rb'].'</td>
					</tr>
					<tr>
						<td align="right">Deductible :</td>
						<td>'.number_format($policyData['deductible'],2,'.',',').'-'.$paInfo['deducper'].'</td>
						<td align="right">Co-Payment :</td>
						<td>'.$policyData['copayment'].'</td>
					</tr>
					<tr>
						<td valign="top" align="right" >รายการ Deductible คงเหลือ :</td>	
						<td colspan="3" align="left">
							<div id="tbl-DeducList_container">
							<table width="70%" align="left" border="0" cellpadding="0" cellspacing="0" >';
	
							if($deducList[0]['deduc'] == 0){
						$html2.='<tr>
									<td align="left" >ไม่มี Deductible คงเหลือ</td>
								</tr>';
							}else{
								foreach( $deducList as $value ) {
						$html2.='<tr>
									<td align="left" >'.$value['dgroup'].'=> ( คงเหลือ '.$value['deduc'].' )</td>							
								</tr>';
								}
							}
							
					$html2.='</table>
							</div>
						</td>
					</tr>
					<tr> 
						<td style="text-align:right;">สถานะกรมธรรม์ :</td>
						<td colspan="3" style="text-align:left;"><B>'.$policyData['polstatus_desc'].'</B> </td>								
					</tr>
				</table>
			</div>
			
			<div style="width:700px; float:left; border-style: dashed; border-width:1px; margin-top:15px;">
				<div id="print_preauth_HEALTH_PolicyRule" style="border-style:solid; border-width:0px; width:350px; border-color: green; float:left;">
					<table width="350" align="center" border="1" cellpadding="3" cellspacing="0" style="font-size:14px; margin-top:0px;">
						<tr>			
							<td align="center" style="font-size:16px;" ><U><B>เงื่อนไขกรมธรรม์</B></U></td>
						</tr>	
						<tr>
							<td height="150" align="center" valign="top">'.$paInfo['policy_condition'].'</td>
						</tr>
					</table>
				</div>
				<div id="print_preauth_HEALTH_PolicyRule1" style="border-style:solid; border-width:0px; width:350px; border-color: green; float:right; ">
					<table width="350" align="center" border="1" cellpadding="3" cellspacing="0" style="font-size:14px; margin-top:0px;">
						<tr>			
							<td align="center" style="font-size:16px;" ><U><B>Exclusion</B></U></td>
						</tr>	
						<tr>
							<td height="150" align="center" valign="top">'.$policyData['exclusion'].'</td>
						</tr>
					</table>
				</div>
			</div>
									
			<div id="print_preauth_HEALTH_Note" style="border-style:solid; border-width:0px; width:700px; border-color: Red; float:left">
				<table width="700" align="center" border="1" cellpadding="3" cellspacing="0" style="font-size:14px; margin-top:15px;" >
					<tr>
						<td align="center" style="font-size:16px;"><U><B>บันทึกเกี่ยวกับ ผอป.</B></U></td>
					</tr>
					<tr>
						<td height="60" align="center" valign="top">'.$paInfo['note2hosp'].'</td>
					</tr>
				</table>
			</div>
			
			<div style="width:700px; height:150px; float:left; border-style: dashed; border-width:0px; margin-top:15px;">
				<div id="print_preauth_HEALTH_History" style="border-style:solid; border-width:0px; width:350px; border-color: yellow;float:left; ">
					<table width="350" align="center" border="1" cellpadding="3" cellspacing="0" style="font-size:14px; margin-top:0px;" >					
						<tr>
							<td colspan="3" align="center" style="font-size:16px;"><U><B>ประวัติการรักษา(ER)</B></U></td>
						</tr>
						<tr>
							<td align="center" valign="bottom">วันเกิดอุบัติเหตุ</td>
							<td align="center" valign="bottom">ค่าใช้จ่าย</td>
							<td align="center" valign="bottom">ICD10</td>
						</tr>';
					foreach( $erClaimData as $er ) {
		$html2.='
						<tr>
							<td align="center">'.$er['acc_date'].'</td>
							<td align="center">'.$er['paid'].' บาท</td>
							<td align="center">'.$er['diags'].'</td>
						</tr>';
					}
		$html2.='   </table>
				</div>
				<div id="print_preauth_HEALTH_History1" style="border-style:solid; border-width:0px; width:350px; border-color: yellow; float:right;">
					<table width="350" align="center" border="1" cellpadding="3" cellspacing="0" style="font-size:14px; margin-top:0px;" >					
						<tr>
							<td colspan="3" align="center" style="font-size:16px;"><U><B>ประวัติการรักษา(OPD)</B></U></td>
						</tr>
						<tr>
							<td align="center" valign="bottom">วันเข้ารับการรักษา</td>
							<td align="center" valign="bottom">โรงพยาบาล</td>
							<td align="center" valign="bottom">ICD10</td>
						</tr>';
					foreach( $OPDClaimData as $value ) {
		$html2	.=	'
						<tr>
							<td align="center">'.$value['acc_date'].'</td>
							<td align="center">'.$value['paid'].' </td>
							<td align="center">'.$value['diags'].'</td>
						</tr>';
					}
		$html2.='   </table>
				</div>
			</div>
	
		</body>
		</html>
';
	/*	
		//$this->render('../../../../MPDF56/mpdf.php');
		$mpdf=new mPDF('utf-8','A4','14','sarabun',5,5,5,5,10,10);
		
		$mpdf->SetDisplayMode('fullpage');	
		// LOAD a stylesheet
		$stylesheet = file_get_contents(APP_HOME."/modules/systemapi/templates/default/css/print.css");
		$mpdf->WriteHTML($stylesheet,1);	// The parameter 1 tells that this is css/style only and no body/html/text
	*/	 
		if($policyData['customer_type_desc'] == "PA"){
			//$mpdf->WriteHTML($html);
			$view->assign("html",$html);
			$SmPDF = $html;
		} else if($policyData['customer_type_desc'] == "Health"){
			//$mpdf->WriteHTML($html2);
			$view->assign("html",$html2);
			$SmPDF = $html2;
		}

		$SmPDF = base64_encode($SmPDF);
		$DataRet = $generic->updatePreauthImage($preauthId,$SmPDF);
		$view->output('pdf/print.tpl');
		
		//$mpdf->SetJS('this.print(false);');
		
		//$SmPDF = $mpdf->Output('', 'S');
		//$SmPDF = base64_encode($SmPDF);
		//$DataRet = $generic->updatePreauthImage($preauthId,$SmPDF);
		
		//$mpdf->Output();
		//exit();

		
    }
    public function printtransAction() {
    	$params   = $this->getParams();
    	$generic = $this->getGeneric();
    	$DataRet = $generic->getPreauthImage($params['preauth_id']);
    	$DataRet = base64_decode($DataRet[preauth_img]);
    	_print($DataRet); 
    	exit();
    }
    
}

?>